package com.efast.cafe.portal.service.company.impl;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.jdbc.JdbcDaoImpl;
import com.efast.cafe.framework.jdbc.persistence.Criteria;
import com.efast.cafe.framework.pager.Pagination;
import com.efast.cafe.framework.service.CafeServiceImpl;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.dao.company.PortalCompanyUserLevelDao;
import com.efast.cafe.portal.entity.company.PortalCompanyUserLevel;
import com.efast.cafe.portal.entity.company.PortalCompanyUserLevelUserDto;
import com.efast.cafe.portal.service.company.IPortalCompanyUserLevelService;
/**
 * ****************************************************************************
 * @File name   :      PortalCompanyUserLevelServiceImpl.java
 *
 * @Author      :      bbqu
 *
 * @Date        :      2016-9-5
 *
 * @Copyright Notice: 
 * Copyright (c) 2016 Efast Serv. All  Rights Reserved.
 * This software is published under the terms of the Efast Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2016-9-5 下午4:53:03        bbqu     1.0            Initial Version
 ****************************************************************************
 */
@Service
@Transactional
public class PortalCompanyUserLevelServiceImpl extends CafeServiceImpl<PortalCompanyUserLevel> implements IPortalCompanyUserLevelService{
	@Qualifier("jdbcDao")
	@Autowired
	private  JdbcDao  jdbcDao;

	private  PortalCompanyUserLevelDao  portalCompanyUserLevelDao;
	
	@Autowired
	public void setPortalCompanyUserLevelDao(PortalCompanyUserLevelDao portalCompanyUserLevelDao) {
		this.portalCompanyUserLevelDao = portalCompanyUserLevelDao;
	}

	
	

	@Override
	public Pagination<PortalCompanyUserLevel> queryPagePortalCompanyUserLevelList(
			Pagination<PortalCompanyUserLevel> pager,
			PortalCompanyUserLevel portalCompanyUserLevel) {
		Criteria criteria = Criteria.create(PortalCompanyUserLevel.class).where(PortalCompanyUserLevel.COMPANY_CODE, new Object[]{SpringWebUtils.getRequestCompany()});
		
		//查询条件
		if(StringUtils.isNotBlank(portalCompanyUserLevel.getLevel_code())){
			criteria.and(PortalCompanyUserLevel.LEVEL_NAME, Criteria.FIELD_OPERATOR_LIKE, new Object[]{"%"+portalCompanyUserLevel.getLevel_code()+"%"});
		}
		//排序
		if(StringUtils.isNotBlank(pager.getOrderBy()) && StringUtils.isNotBlank(pager.getSort())){
			if(pager.getSort().equals("asc"))
				criteria.asc(pager.getOrderBy());
			else
				criteria.desc(pager.getOrderBy());
		}else{
			criteria.desc(PortalCompanyUserLevel.PORTAL_COMPANY_USER_LEVEL_ID);
		}
		return jdbcDao.queryListByPage(pager, criteria);
	}
	@Override
	public PortalCompanyUserLevel findUserLevelById(Long id) {
		return jdbcDao.get(PortalCompanyUserLevel.class, id);
	}
	@Override
	public Long insertUserlevel(PortalCompanyUserLevel portalCompanyUserLevel) {
		Criteria criteria = Criteria.create(PortalCompanyUserLevel.class)
				.where(PortalCompanyUserLevel.LEVEL_CODE, new Object[]{portalCompanyUserLevel.getLevel_code()})
				.and(PortalCompanyUserLevel.COMPANY_CODE, new Object[]{SpringWebUtils.getRequestCompany()});
		int count = jdbcDao.queryCount(criteria);
		if (count!=0) {
			throw new ServiceException("msg.portal.company.org.exist");//该code已经存在
			}
		portalCompanyUserLevel.setCompany_code(SpringWebUtils.getRequestCompany());
		Long num = jdbcDao.insert(portalCompanyUserLevel);
		return num;
	}
	@Override
	public void update(PortalCompanyUserLevel portalCompanyUserLevel) {
		int count  = portalCompanyUserLevelDao.levalCodeIsExistWithId(portalCompanyUserLevel.getPortal_company_user_level_id(), portalCompanyUserLevel.getLevel_code(),SpringWebUtils.getRequestCompany());
		if (count !=0 ) {
			throw new ServiceException("msg.portal.company.org.exist");//该code已经存在
		}
		Criteria criteria = Criteria.create(PortalCompanyUserLevel.class)
				.set(PortalCompanyUserLevel.LEVEL_CODE, portalCompanyUserLevel.getLevel_code())
				.set(PortalCompanyUserLevel.LEVEL_DESC, portalCompanyUserLevel.getLevel_desc())
				.set(PortalCompanyUserLevel.LEVEL_NAME, portalCompanyUserLevel.getLevel_name())
				.where(PortalCompanyUserLevel.PORTAL_COMPANY_USER_LEVEL_ID,new Object[]{portalCompanyUserLevel.getPortal_company_user_level_id()})
				.and(PortalCompanyUserLevel.COMPANY_CODE, new Object[]{SpringWebUtils.getRequestCompany()});
		jdbcDao.update(criteria);
	}
	@Override
	public void del(Long id) {
		jdbcDao.delete(PortalCompanyUserLevel.class, id);
		
	}
	@Override
	public List<PortalCompanyUserLevel> listUserLevels() {
		Criteria criteria = Criteria.create(PortalCompanyUserLevel.class).where(PortalCompanyUserLevel.COMPANY_CODE, new Object[]{SpringWebUtils.getRequestCompany()});;
		return jdbcDao.queryList(criteria);
	}

	@Override
	public List<PortalCompanyUserLevel> userLevelAll() {
		return portalCompanyUserLevelDao.queryUserLevelAll(SpringWebUtils.getRequestCompany());
	}
	
	@Override
	public List<PortalCompanyUserLevelUserDto> levelUserList(Long portal_company_user_level_id){
		return portalCompanyUserLevelDao.queryUserLevelUser(portal_company_user_level_id,SpringWebUtils.getRequestCompany());
	}
	
	
}
